package com.sgb.kjwl.view.ui.activity.self.logistics;

import android.content.Context;
import android.content.Intent;
import androidx.databinding.DataBindingUtil;
import androidx.core.content.ContextCompat;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.util.Log;
import android.view.View;

import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityCarriageBusinessBinding;
import com.sgb.kjwl.di.self.DaggerSelfComponent;
import com.sgb.kjwl.di.self.SelfModule;
import com.sgb.kjwl.model.entity.self.CarriageBusinessEntity;
import com.sgb.kjwl.view.ui.adapter.self.logistics.BusinessAdapter;
import com.sgb.kjwl.viewmodel.self.logistics.CarriageBusinessModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.util.List;

import javax.inject.Inject;

/*
*   说明：承运商机  （物流运输  货主）
*   作者：md
 */
public class CarriageBusinessActivity extends AppActivity {


    ActivityCarriageBusinessBinding binding;
    private BusinessAdapter adapter;

    @Inject
    CarriageBusinessModel viewModel;
    List<CarriageBusinessEntity> arrayList;


    public static void start(Context context) {
        Intent intent = new Intent(context, CarriageBusinessActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        context.startActivity(intent);
    }



    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
         DaggerSelfComponent.builder().appComponent(appComponent).selfModule(new SelfModule(this)).build().inject(this);
        Log.e("CarriageBusiness","1");
    }


    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(CarriageBusinessActivity.this, R.layout.activity_carriage_business);
        binding.include.titleView.setText("承运商机");
        binding.include.rightView.setVisibility(View.VISIBLE);
        binding.include.rightView.setOnClickListener(this);
        binding.include.ivBack.setOnClickListener(this);
        Log.e("CarriageBusiness","2");
        if(adapter==null){
             adapter = new BusinessAdapter(getActivity(), null,viewModel.repertory.getAreaCode());
        }
        DividerItemDecoration divider = new DividerItemDecoration(this, DividerItemDecoration.VERTICAL);
        divider.setDrawable(ContextCompat.getDrawable(this, R.drawable.shape_list_divide));
        binding.recyclerView.addItemDecoration(divider);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        binding.recyclerView.setLayoutManager(layoutManager);
        binding.recyclerView.setAdapter(adapter);
        //刷新的一些操作
        binding.recyclerView.setLoadingMoreEnabled(true);
//        binding.recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
//            @Override
//            public void onRefresh() {
//                viewModel.getCarriageBusinessModel();
//
//            }
//
//            @Override
//            public void onLoadMore() {
//                viewModel.getCarriageBusinessModel();
//
//            }
//        });

        adapter.setOnItemClick(new AdapterOnItemClick<CarriageBusinessEntity>() {
            @Override
            public void onItemClick(CarriageBusinessEntity carriageBusinessEntity, int position) {

            }
        });


    }

    @Override
    protected void init() {
        Log.e("CarriageBusiness","7");

    }


    @Override
    protected void onResume() {
        super.onResume();
        Log.e("test_CargoManage","4");
        // TODO: 2021/1/18 1.7.3暂时显示空数据
//        viewModel.getCarriageBusinessModel();
        binding.emptyView.setVisibility(View.VISIBLE);
        binding.recyclerView.setVisibility(View.GONE);
    }



    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                Log.e("CarriageBusiness","8");
                finish();
                break;
        }
    }


    public void showCarriageBusiness(List<CarriageBusinessEntity> arrayList) {

        this.arrayList = arrayList;
        binding.recyclerView.refreshComplete();
        binding.emptyView.setVisibility(arrayList.size() == 0 ? View.VISIBLE : View.GONE);
        adapter.setDatas(arrayList);


    }
}